package com.lin.config;

import com.lin.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Created by Lin Weihong
 * @date on 2022/4/14 23:12
 */
@Component
@Slf4j
public class AsyncConfig {
    @Resource
    RedisUtil redisUtil;

    @Async
//    @Scheduled(cron = "0 0 */1 * * ?")
    @Scheduled(cron = "0 */30 * * * ?")
    public void findDailyNews() throws IOException {
        log.info("*******************进入了新闻爬虫");
        LinkedHashMap<String, String> hashMap = new LinkedHashMap<String, String>();
        Document doc = Jsoup.parse(new URL("https://news.baidu.com/"), 1000);
        Element element = doc.getElementsByClass("mod-tab-pane active").first();
        String text = element.text();
        String[] everyNews = text.split("\\s+");
        Elements li = element.select("li");
        for (int i = 0; i < li.size(); i++) {
            String text2 = li.get(i).text();
            String attr = li.get(i).select("a").attr("href");
            hashMap.put(text2, attr);
        }
        for (Map.Entry<String, String> map : hashMap.entrySet()) {
            redisUtil.hset("hotnews", map.getKey(), map.getValue(), 1800);
        }
    }
}
